package com.namo.cchapi.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.namo.cchapi.entity.baidu.ocr.IdCardEntity;
import com.namo.cchapi.service.OcrService;
import com.namo.cchcommon.api.CommonResult;
import com.sun.org.apache.xpath.internal.operations.Or;
import jdk.nashorn.internal.runtime.JSErrorType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

/**
 * @author WangXin
 * @date 2021/4/10 14:49
 */
@RestController
@RequestMapping("/baidu/ocr")
@Slf4j
public class OcrController {
    @Autowired
    OcrService ocrService;
    @Autowired
    Gson gson;

    public static final String IMAGE = "image";
    public static final String IMAGE_TYPE = "image_type";
    public static final String IMAGE_SIDE = "image_side";


    /**
     * ocr识别身份证
     * @param reqBody
     * @return
     */
    @RequestMapping(value = "/id_card",method = RequestMethod.POST)
    public CommonResult ocrIdCard(@RequestBody String reqBody){
        JsonObject reqObject = gson.fromJson(reqBody,JsonObject.class);
        if (reqObject.has(IMAGE) == false || reqObject.has(IMAGE_TYPE) == false ||reqObject.has(IMAGE_SIDE) == false){
            log.warn("缺少必要参数=>"+reqObject);
            return CommonResult.failed("缺少必要参数！");
        }
        IdCardEntity idCardEntity = null;
        try {
            idCardEntity = ocrService.ocrIdCard(reqObject.get("image").getAsString(),reqObject.get("image_type").getAsString(),reqObject.get("image_side").getAsString());
        } catch (Exception e) {
            e.printStackTrace();
            //失败返回
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success(idCardEntity);
    }







}
